//
//  RideDataModel.swift
//  TimeBoat
//
//  Created by Jun on 2022/9/8.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

/// 骑行
@objcMembers class RideDataModel: NSObject {
    /// 模式
    var model: UInt8 = 0

    /// 完成指标
    var target: UInt8 = 0

    /// 计划里程
    var planeDistance: UInt32 = 0

    /// 计划用时
    var planeUseTime: UInt32 = 0

    /// 实际里程
    var actualDistance: UInt32 = 0

    /// 实际用时
    var actualUseTime: UInt32 = 0

    /// 起始点经度
    var startLng: UInt32 = 0

    /// 起始点纬度
    var startLat: UInt32 = 0

    /// 经度数据数组 经度增量，精度 0.00001 度,每 5 秒钟记录一次数据
    var lngArray: [UInt8]? = nil

    /// 纬度数据数组 纬度增量，精度 0.00001 度,每 5 秒钟记录一次数据
    var latArray: [UInt8]? = nil

    /// 速度数组
    var speedArray: [UInt8]? = nil

    /// 海拔高度数组 单位:秒, 每公里所用时间，每公里记录一次数据
    var altitudeList: [UInt16]? = nil

    /// 配速数组
    var paceList: [UInt16]? = nil

    /// 心率测量数组
    var heartRateList: [UInt8]? = nil

    /// 卡路里
    var calorie: UInt32 = 0

    /// 总计上坡高度
    var totalUphillHeight: UInt32 = 0
    
    convenience init(buf: [UInt8]) {
        self.init()
        
        var index = 0
        
        if (index + 1 <= buf.count) {
            // 模式 1byte
            model = buf[index]
            index += 1
        }

        if (index + 1 <= buf.count) {
            // 完成指标 1byte
            target = buf[index]
            index += 1
        }

        if (index + 4 <= buf.count) {
            // 计划里程 4byte
            planeDistance = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 计划用时  4byte
            planeUseTime = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            //实际里程
            actualDistance = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            //实际用时
            actualUseTime = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            //起始点经度
            startLng = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            //起始点纬度
            startLat = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 2 <= buf.count) {
            //经度数据数组 经度增量，精度 0.00001 度,每 5 秒钟记录一次数据
            let lngArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (lngArrayLength > 0) {
                if (index + lngArrayLength <= buf.count) {
                    lngArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: lngArrayLength)
                    index += lngArrayLength
                } else {
                    Log.debug("index + lngArrayLength is overflow, \(index), \(lngArrayLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            //纬度数据数组 纬度增量，精度 0.00001 度,每 5 秒钟记录一次数据
            let latArrayLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (latArrayLength > 0) {
                if (index + latArrayLength <= buf.count) {
                    latArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: latArrayLength)
                    index += latArrayLength
                } else {
                    Log.debug("index + latArrayLength is overflow, \(index), \(latArrayLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            //速度数组
            let speedLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (speedLength > 0) {
                if (index + speedLength <= buf.count) {
                    speedArray = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: speedLength)
                    index += speedLength
                } else {
                    Log.debug("index + speedLength is overflow, \(index), \(speedLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            //海拔高度数组 单位:秒, 每公里所用时间，每公里记录一次数据
            let altitudeLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (altitudeLength > 0) {
                if (index + altitudeLength * 2 <= buf.count) {
                    altitudeList = [UInt16]()
                    for _ in 0...(altitudeLength-1) {
                        let data = UInt16.toUInt16(buf: buf, offset: index)
                        index += 2
                        altitudeList?.append(data)
                    }
                } else {
                    Log.debug("index + altitudeLength is overflow, \(index), \(altitudeLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            //配速数组
            let paceLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (paceLength > 0) {
                if (index + paceLength * 2 <= buf.count) {
                    paceList = [UInt16]()
                    for _ in 0...(paceLength-1) {
                        let data = UInt16.toUInt16(buf: buf, offset: index)
                        index += 2
                        paceList?.append(data)
                    }
                } else {
                    Log.debug("index + paceLength is overflow, \(index), \(paceLength)")
                }
            }
        }

        if (index + 2 <= buf.count) {
            //心率测量数组
            let heartRateLength = Int(UInt16.toUInt16(buf: buf, offset: index))
            index += 2

            if (heartRateLength > 0) {
                if (index + heartRateLength <= buf.count) {
                    heartRateList = Array<UInt8>.copyBuffer(buffer: buf, offset: index, length: heartRateLength)
                    index += heartRateLength
                } else {
                    Log.debug("index + heartRateLength is overflow, \(index), \(heartRateLength)")
                }
            }
        }

        if (index + 4 <= buf.count) {
            //卡路里
            calorie = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }

        if (index + 4 <= buf.count) {
            // 总计上坡高度
            totalUphillHeight = UInt32.toUInt32(buf: buf, offset: index)
            index += 4
        }
    }

    @objc func toString() -> String {
        var str = self.yy_modelToJSONString()
        if nil == str {
            str = ""
        }
        
        return str!
    }
}
